package com.webchat.admin.service.sys;

import com.webchat.common.bean.APIPageResponseBean;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.exception.BusinessException;
import com.webchat.domain.vo.request.sys.SysDataDictionaryDetailRequestVo;
import com.webchat.domain.vo.request.sys.SysDataDictionaryRequestVO;
import com.webchat.domain.vo.response.sys.SysDataDictionaryDetailBaseVO;
import com.webchat.domain.vo.response.sys.SysDataDictionaryResponseVO;
import com.webchat.rmi.pgc.sys.SysDataDictionaryClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 数据字典服务
 *
 * @author SongRenShuo
 * @date 2025/06/28
 */
@Service
public class SysDataDictionaryService {

    @Autowired
    private SysDataDictionaryClient sysDataDictionaryClient;


    /**
     * 创建或者更新数据字典
     *
     * @param requestVO 请求 VO
     * @return {@link Long } 主键
     */
    public Long save(SysDataDictionaryRequestVO requestVO) {

        APIResponseBean<Long> apiResponseBean = sysDataDictionaryClient.save(requestVO);
        if (APIResponseBeanUtil.isOk(apiResponseBean)) {
            return apiResponseBean.getData();
        }
        throw new BusinessException(apiResponseBean.getMsg());
    }

    /**
     * 删除数据字典
     *
     * @param dictionaryId 数据字典 ID
     * @param userId     用户ID
     * @return boolean
     */
    public boolean delete(Long dictionaryId, String userId) {

        APIResponseBean<Boolean> apiResponseBean = sysDataDictionaryClient.delete(dictionaryId, userId);
        if (APIResponseBeanUtil.isOk(apiResponseBean)) {
            return apiResponseBean.getData();
        }
        throw new BusinessException(apiResponseBean.getMsg());
    }

    /**
     * 分页查询数据字典
     *
     * @param categoryId     分类 ID
     * @param code     编号
     * @param name     名称
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @return {@link APIPageResponseBean }<{@link SysDataDictionaryResponseVO }>
     */
    public APIPageResponseBean<SysDataDictionaryResponseVO> page(Long categoryId, String code, String name,
                                                                 Integer pageNo, Integer pageSize) {

        return sysDataDictionaryClient.page(categoryId,code, name, pageNo, pageSize);
    }

    /**
     * 创建或者更新数据字典详情
     *
     * @param requestVO 请求 VO
     * @return {@link Long } 主键
     */
    public Long saveDetail(SysDataDictionaryDetailRequestVo requestVO) {

        APIResponseBean<Long> apiResponseBean = sysDataDictionaryClient.saveDetail(requestVO);
        if (APIResponseBeanUtil.isOk(apiResponseBean)) {
            return apiResponseBean.getData();
        }
        throw new BusinessException(apiResponseBean.getMsg());
    }

    /**
     * 删除数据字典详情
     *
     * @param detailIdId 详情 ID
     * @param userId     用户ID
     * @return boolean
     */
    public boolean deleteDetail(Long detailIdId, String userId) {

        APIResponseBean<Boolean> apiResponseBean = sysDataDictionaryClient.deleteDetail(detailIdId, userId);
        if (APIResponseBeanUtil.isOk(apiResponseBean)) {
            return apiResponseBean.getData();
        }
        throw new BusinessException(apiResponseBean.getMsg());
    }

    /**
     * 分页查询数据字典详情
     *
     * @param dataDictionaryId     数据字典ID
     * @param code     编号
     * @param name     名称
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @return {@link APIPageResponseBean }<{@link SysDataDictionaryDetailBaseVO }>
     */
    public APIPageResponseBean<SysDataDictionaryDetailBaseVO> pageDetail(Long dataDictionaryId, String code, String name,
                                                                         Integer pageNo, Integer pageSize) {

        return sysDataDictionaryClient.pageDetail(dataDictionaryId,code, name, pageNo, pageSize);
    }

}
